package org.leaf.green.b2b

import org.springframework.transaction.annotation.*
import org.hibernate.FetchMode as FM
import org.hibernate.criterion.CriteriaSpecification

class StockOutAjaxService {

    static transactional = true

    //get staff list
    @Transactional(readOnly = true)
    def staffList(term) {
        log.info("staffList")
        def staffList = Staff.withCriteria{
            projections{
                property('id')
                property('serialNumber')
                property('name')
            }
            or{
                ilike("serialNumber", term + "%")
                ilike("name", "%"+term + "%")
            }
            maxResults(10)
            order("serialNumber", "asc")
        }

        return staffList
    }

    //get retailer
    @Transactional(readOnly = true)
    def retailer(Long retailerId) {
        log.info("retailer")
        def retailer = Retailer.withCriteria(uniqueResult:true){
            projections{
                property('serialNumber')
                property('name')
            }
            
            eq("id", retailerId)
        }

        return "${retailer[0]}-${retailer[1]}"
    }

    //get distribution
    @Transactional(readOnly = true)
    def distribution(Long distributionId) {
        log.info("distribution")
        def distribution = Distribution.withCriteria(uniqueResult:true){
            projections{
                property('serialNumber')
            }
            
            eq("id", distributionId)
        }

        return "${distribution}"
    }    

    //product,$serialNumber-$name
    @Transactional(readOnly = true)
    def product(Long Id) {
        def product = Product.withCriteria(uniqueResult:true){
            projections{
                property("serialNumber")
                property("name")
            }
            eq("id", Id)
        }

        return "${product[0]}-${product[1]}"
    }
    //distribution detail
    @Transactional(readOnly = true)
    def distributionDetail(Long Id) {
        log.info("Id=${Id}")
        def distributionDetail_serialNumber = DistributionDetail.withCriteria(uniqueResult:true){

            createAlias 'distribution', 'dist'

            projections{
                property("dist.serialNumber")
                property("serialNumber")
            }

            fetchMode "distribution", FM.JOIN

            eq("id", Id)
        }
        log.info("${distributionDetail_serialNumber}")
        return "${distributionDetail_serialNumber[0]}-${distributionDetail_serialNumber[1]}"
    }

    def availableProductInventory(Long Id){
        log.info("availableProductInventory")
        def stockInDetails = StockInDetail.withCriteria(uniqueResult:true){

            createAlias 'product', 'prod', CriteriaSpecification.LEFT_JOIN
            createAlias 'stockIn', 'master', CriteriaSpecification.LEFT_JOIN

            projections{
                property("master.serialNumber")
                property("serialNumber")
                property("quantity")
                property("used")
                property("surplus")
                property("life")
            }
           
            fetchMode "product", FM.JOIN
            fetchMode "stockIn", FM.JOIN

            eq("id", Id)
            eq("master.approved", true)
            eq("isOver", false)

            order("life", "asc")
        }
        return stockInDetails
    }    
}
